package edu.cn.nlsde.tmfst.web.controller;

import edu.cn.nlsde.tmfst.model.Comment;
import edu.cn.nlsde.tmfst.textCrawler.CommentCrawler;
import edu.cn.nlsde.tmfst.textCrawler.JDCommentCrawler;
import edu.cn.nlsde.tmfst.textCrawler.TmallCommentCrawler;
import edu.cn.nlsde.tmfst.web.common.CommentResult;
import edu.cn.nlsde.tmfst.web.common.ResponseResult;
import main.RWNTM;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by dell on 2016/12/13.
 */

@Controller
@RequestMapping("/Topic")
public class TopicController {
    @RequestMapping(value = "/main", method = RequestMethod.GET)
    public String main() {
        return "Topic/main";
    }

    @RequestMapping(value = "/all", method = RequestMethod.GET)
    public String all() {
        return "Topic/all";
    }

    @RequestMapping(value = "/topic", method = RequestMethod.GET)
    public String topic() {
        return "Topic/topic";
    }

    @RequestMapping(value = "/document", method = RequestMethod.GET)
    public String document() {
        return "Topic/document";
    }

    @RequestMapping(value = "/word", method = RequestMethod.GET)
    public String word() {
        return "Topic/word";
    }


    @ResponseBody
    @RequestMapping(value = "/handle", method = RequestMethod.POST)
    public CommentResult handle(String url) {
        url = url.replaceAll("\\{[^\\{\\}]*?\\}", "");
        List<Comment> list = null;
        CommentResult result = new CommentResult();
        if (url.startsWith("Tmall_") || url.startsWith("JD_")) {
            list = Comment.get_comment(url);
        } else {
            try {
                CommentCrawler cc = new TmallCommentCrawler();
                list = cc.get_Comment_list(url);
                System.out.println("finished get comment");
                if (list == null) {
                    cc = new JDCommentCrawler();
                    list = cc.get_Comment_list(url);
                }
                System.out.println("finished get comment");
            } catch (Exception e) {
                e.printStackTrace();
                result.status = false;
            }
            System.out.println("finished");
        }
        if (list.size() < 30) {
            result.status = false;
            result.msg = "爬取评论不足";
        } else {
            RWNTM process = new RWNTM();
            result.product_id = list.get(0).product_id;
            result.model = process.trainData(list);
            result.status = true;

        }
        return result;
    }

    @ResponseBody
    @RequestMapping(value = "/topicShow", method = RequestMethod.GET)
    public String topicShow() {
        ResponseResult result = new ResponseResult();
        try {
            Thread.sleep(3000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        return "";
    }

}
